const chalk = require('chalk')
const fs = require('fs')
const path = require('path')
const basePath = path.resolve(__dirname, '../abc')

const handlebars = require('handlebars')
module.exports = async () => {
  const list = fs
    .readdirSync(basePath + '/src/views')
    .filter((v) => v !== 'Home.vue')
    .map((v) => {
      return {
        name: v.replace('.vue', '').toLowerCase(),
        file: `/${v}`,
      }
    })

  complite(
    { list },
    basePath + '/src/router.js',
    basePath + '/template/router.js.hbs'
  )
  complite(
    { list },
    basePath + '/src/App.vue',
    basePath + '/template/App.vue.hbs'
  )

  function complite(meta, filePath, template) {
    // console.log(fs.existsSync('template', template))
    if (fs.existsSync(template)) {
      const content = fs.readFileSync(template).toString()
      const result = handlebars.compile(content)(meta)
      fs.writeFileSync(filePath, result)

      console.log('创建成功')
    }
  }
}
